package com.supereye.platform.web.sys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.supereye.platform.common.ResultMsg;
import com.supereye.platform.common.SysConstant;
import com.supereye.platform.util.EncodeUtil;
import com.supereye.platform.web.sys.model.SysUser;
import com.supereye.platform.web.sys.service.SysUserService;

@Controller
@RequestMapping("/sysUser")
public class SysUserController {
	@Resource 
	private SysUserService sysUserService;
	
	@RequestMapping("/addUser")
	@ResponseBody
	public ResultMsg addUser(HttpServletRequest request,HttpServletResponse response,SysUser user) throws Exception{
		try{
			sysUserService.addUser(user);
			return new ResultMsg(ResultMsg.SUCCESS,"新增成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"新增失败");
		}
	}
	
	@RequestMapping("/updateUser")
	@ResponseBody
	public ResultMsg updateUser(HttpServletRequest request,HttpServletResponse response,SysUser user) throws Exception{
		try{
			sysUserService.updateUser(user);
			return new ResultMsg(ResultMsg.SUCCESS,"修改成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"修改失败");
		}
	}
	
	@RequestMapping("/getUserList")
	@ResponseBody
	public String getUserList(HttpServletRequest request,HttpServletResponse response,SysUser user) throws Exception{
		String result = sysUserService.getUserList(user);
		return result;
	}
	
	@RequestMapping("/getUserListNotAuth")
	@ResponseBody
	public String getUserListNotAuth(HttpServletRequest request,HttpServletResponse response,SysUser user) throws Exception{
		String result = sysUserService.getUserListNotAuth();
		return result;
	}
	
	@RequestMapping("/deleteUsers")
	@ResponseBody
	public ResultMsg deleteUsers(HttpServletRequest request,HttpServletResponse response,String userIds) throws Exception{
		try{
			sysUserService.deleteUsers(userIds.split(","));
			return new ResultMsg(ResultMsg.SUCCESS,"删除成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"删除失败");
		}
	}
	
	@RequestMapping("/getUserAuthList")
	@ResponseBody
	public String getUserAuthList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String orgId = request.getParameter("orgId");
		String result = sysUserService.getUserAuthList(Integer.parseInt(orgId));
		return result;
	}
	
	@RequestMapping("/addUserAuth")
	@ResponseBody
	public ResultMsg addUserAuth(HttpServletRequest request,HttpServletResponse response,Integer orgId,String userIds) throws Exception{
		try{
			sysUserService.addUserAuth(orgId,userIds);
			return new ResultMsg(ResultMsg.SUCCESS,"新增成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"新增失败");
		}
	}
	
	@RequestMapping("/deleteUserAuth")
	@ResponseBody
	public ResultMsg deleteUserAuth(HttpServletRequest request,HttpServletResponse response,Integer orgId,String userIds) throws Exception {
		try{
			sysUserService.deleteUserAuth(orgId,userIds);
			return new ResultMsg(ResultMsg.SUCCESS,"删除成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"删除失败");
		}
	}
	
	@RequestMapping("/updatePassword")
	@ResponseBody
	public ResultMsg updatePassword(HttpServletRequest request,HttpServletResponse response,SysUser user,String newpassword) throws Exception{
		try{
			if(request.getSession().getAttribute(SysConstant.LOGIN_USER) == null){
				return new ResultMsg(ResultMsg.FAIL,"用户未登录！");
			}
			
			SysUser loginUser = (SysUser) request.getSession().getAttribute(SysConstant.LOGIN_USER);
			if(!loginUser.getPassword().equals(EncodeUtil.toMd5(user.getPassword()))){
				return new ResultMsg(ResultMsg.FAIL,"旧密码输入错误");
			}else{
				sysUserService.updatePassword(loginUser.getId(),EncodeUtil.toMd5(newpassword));
				return new ResultMsg(ResultMsg.SUCCESS,"修改成功");
			}
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"修改失败");
		}
	}
	
}
